package com.itheima;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@WebServlet("/servletDemo3")
public class ServletDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        String realPath = servletContext.getRealPath("stu.txt");
        BufferedReader bis=new BufferedReader(new FileReader(realPath));
        ServletOutputStream outputStream = response.getOutputStream();
        request.setCharacterEncoding("utf-8");
        String s;
        while ((s=bis.readLine())!=null){
            outputStream.write(s.getBytes());
        }
        bis.close();
    }
}